package cn.tedu.ivos.audit.service.impl;

import cn.tedu.ivos.application.mapper.ApplicationMapper;
import cn.tedu.ivos.application.pojo.entity.Application;
import cn.tedu.ivos.audit.mapper.AuditMapper;
import cn.tedu.ivos.audit.pojo.dto.AuditSaveParam;
import cn.tedu.ivos.audit.pojo.dto.AuditSaveParam1;
import cn.tedu.ivos.audit.pojo.entity.Audit;
import cn.tedu.ivos.audit.pojo.vo.AuditVo;
import cn.tedu.ivos.audit.service.AuditService;
import cn.tedu.ivos.base.enums.ApplicationStatusEnum;
import cn.tedu.ivos.base.enums.AuditStatusEnum;
import cn.tedu.ivos.user.mapper.UserMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.StringJoiner;

@Slf4j
@Service
public class AuditServiceImpl implements AuditService {
    @Autowired
    AuditMapper auditMapper;
    @Autowired
    UserMapper userMapper;
    @Autowired
    ApplicationMapper applicationMapper;

    @Override
    public void insert(Application application) {
        log.debug("为当前申请单生成对应审批单的业务: {}", application);
        List<Long> auditUserIdList = application.getAuditUserIdList();
        for (int i=0; i < auditUserIdList.size(); i++) {
            Audit audit = new Audit();
            audit.setApplicationId(application.getId());
            audit.setAuditUserId(auditUserIdList.get(i));
            audit.setAuditSort(i);
            if (i == 0){
                audit.setAuditStatus(AuditStatusEnum.MY_PENDING.getCode());
            }else {
                audit.setAuditStatus(AuditStatusEnum.PENDING.getCode());
            }
            audit.setCreateTime(new Date());
            auditMapper.insert(audit);
        }
        log.debug("为当前申请单生成对应审批单业务结束");
    }

    @Override
    public List<AuditVo> selectAuditByApplicationId(Long applicationId) {
        log.debug("查询当前申请单的审批单业务: {}", applicationId);
        List<AuditVo> audits = auditMapper.selectAuditByApplicationId(applicationId);
        return audits;

    }

    @Override
    public void deleteAuditByApplicationId(Long id) {
        log.debug("删除当前申请单的审批单业务: {}", id);
        auditMapper.deleteAuditByApplicationId(id);
        log.debug("删除当前申请单的审批单业务结束");
    }

    @Override
    public List<AuditVo> selectAudit(AuditSaveParam auditSaveParam) {
        log.debug("查询审批单业务: {}",auditSaveParam);
        List<AuditVo> audits=auditMapper.selectAudit(auditSaveParam);
        log.debug("查询审批单业务返回: {}",audits);
        for (int i = 0; i < audits.size(); i++) {
            AuditVo auditVo = audits.get(i);
            assignAuditUserList(auditVo);
        }
        return audits;
    }
    private void assignAuditUserList(AuditVo auditVo){
        List<Long> auditUserIdList=new ArrayList<>();
        List<String> auditUserNameList=new ArrayList<>();
        List<AuditVo> auditVoList=auditMapper.selectAuditByApplicationId(auditVo.getApplicationId());
        for (int i = 0; i <auditVoList.size() ; i++) {
            AuditVo auditVo1 = auditVoList.get(i);
            Long id = auditVo1.getAuditUserId();
            auditUserIdList.add(id);
            userMapper.selectAudit(id);
            auditUserNameList.add(userMapper.selectAudit(id).getUsername());
        }
        log.debug("auditUserNameList2: {}",auditUserNameList);
        StringJoiner stringJoiner=new StringJoiner(",");
        for (String username : auditUserNameList){
            stringJoiner.add(username);
        }
        log.debug("stringJoiner: {}",stringJoiner);
        auditVo.setAuditUserIdList(auditUserIdList);
        auditVo.setAuditUsernameList(stringJoiner.toString());
    }

    @Override
    public void updateAudit(AuditSaveParam1 auditSaveParam1) {
        log.debug("更新审批单业务: {}",auditSaveParam1);
        Audit audit=new Audit();
        BeanUtils.copyProperties(auditSaveParam1,audit);
        audit.setUpdateTime(new Date());
        auditMapper.updateAudit(audit);
        log.debug("更新审批单业务结束: {}",audit);
        Application application=new Application();
        application.setId(audit.getApplicationId());
        application.setUpdateTime(new Date());
        if (audit.getAuditStatus().equals(AuditStatusEnum.AUDITED.getCode())){
            AuditSaveParam auditSaveParam=new AuditSaveParam();
            auditSaveParam.setApplicationId(audit.getApplicationId());
            Integer count=auditMapper.selectRestAuditCount(auditSaveParam);
            if (count>0){
                auditSaveParam.setAuditSort(audit.getAuditSort()+1);
                List<AuditVo> auditVoList = auditMapper.selectAudit(auditSaveParam);
                if (auditVoList.size()>0 && auditVoList!=null){
                    AuditVo auditVo = auditVoList.get(0);
                    Audit audit2=new Audit();
                    audit2.setId(auditVo.getId());
                    audit2.setAuditStatus(AuditStatusEnum.MY_PENDING.getCode());
                    audit2.setUpdateTime(new Date());
                    auditMapper.updateAudit(audit2);
                }
                application.setStatus(ApplicationStatusEnum.AUDIT.getCode());
                applicationMapper.updateStatus(application);
            }else {
                application.setStatus(AuditStatusEnum.AUDITED.getCode());
                applicationMapper.updateStatus(application);
            }
        }else if (audit.getAuditStatus().equals(AuditStatusEnum.REJECT.getCode())){
            AuditSaveParam auditSaveParam=new AuditSaveParam();
            auditSaveParam.setApplicationId(audit.getApplicationId());
            List<AuditVo> auditVoList = auditMapper.selectAudit(auditSaveParam);
            if (auditVoList.size()>0 && auditVoList!=null){
                AuditVo auditVo = auditVoList.get(0);
                if (auditVo.getAuditStatus().equals(AuditStatusEnum.PENDING.getCode())){
                    auditMapper.deleteById(auditVo.getId());
                }
            }
        }
        application.setRejectReason(auditSaveParam1.getRejectReason());
        application.setStatus(ApplicationStatusEnum.REJECT.getCode());
        applicationMapper.updateStatus(application);
    }
}
